<?php
	session_start();
	if ($_SESSION['logd'] != 1){
	    Header("Location:login.php");
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Message Hub</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<script type="text/javascript">
		function ask2confirm(){
			var answer=confirm("Do you really want to delete this Message?");
			if (answer == true){
				return true;
			} else {
			    return false;
			}
		}
	</script>
    </head>

    <body>

	<!-- Begin Wrapper -->
	<div id="wrapper">

	    <!-- Begin Header -->
	    <div id="header">
		<?php
		include("include/header.html");
		?>	
	    </div>
	    <!-- End Header -->

	    <!-- Begin Faux Columns -->
	    <div id="faux">

		<!-- Begin Left Column -->
		<div id="leftcolumn">
		    <?php
		    include("include/menu.php");
		    ?>
		</div>
		<!-- End Left Column -->

		<!-- Begin Right Column -->
		<div id="rightcolumn">
		    <div id="tagcloud">
			<?php
				include("scripts/dbconnect.php");
				//delete messages
				$post2Delete			= $_POST['postId'];
				if (isset($post2Delete)){
					$checkQuery		= "SELECT * FROM Messages WHERE MsgId = '".$post2Delete."'";
					$checkExec		= $db->query($checkQuery);
					$checkRow		= $checkExec->fetch_assoc();

					if ($checkRow['SenderPeId'] == $_SESSION['PeId']){
					    $deleteQuery1	= "DELETE FROM Messages WHERE MsgId = '".$post2Delete."'";
					    $deleteQuery2	= "DELETE FROM Tags WHERE MsgId = '".$post2Delete."'";
					    $deleteQuery3	= "DELETE FROM TagNames
									WHERE TagNames.TaNaId NOT IN
									(
									    SELECT Tags.TaNaId FROM Tags
									)";
					    $deleteExec1	= $db->query($deleteQuery1);
					    $deleteExec2	= $db->query($deleteQuery2);
					    $deleteExec3	= $db->query($deleteQuery3);
					}
				}

				//Show the tagcloud
				$query				= "SELECT DISTINCT Tag FROM TagNames WHERE TaNaId in(
								  	SELECT DISTINCT TaNaId FROM Tags WHERE MsgId in
								  		(SELECT MsgId FROM Messages WHERE SenderPeId = '".$_SESSION['PeId']."'))";
				$exec				= $db->query($query);

				while($row			= mysqli_fetch_array($exec)){
				    echo "<a href=\"mysentmessages.php?tag=".stripslashes($row['Tag'])."\">".stripslashes($row['Tag'])."</a> ";
				}
				echo "<hr />";
			?>
		    </div>

		    <div id="posts">
		    	<?php
				$set				= $_GET['tag'];

				if (isset($set)){
				    $query			= "SELECT * FROM Messages
								  LEFT JOIN (Tags, TagNames)
								  	ON (Messages.MsgId = Tags.MsgId AND Tags.TaNaId = TagNames.TaNaId)
								  WHERE TagNames.Tag = '".$set."' AND SenderPeId = '".$_SESSION['PeId']."'
								  GROUP BY Messages.MsgId
								  ORDER BY TimeStamp DESC";
				} else {
				    $query			= "SELECT * FROM Messages WHERE SenderPeId = '".$_SESSION['PeId']."'";
				}

				include("scripts/dbconnect.php");
				$exec				= $db->query($query);

				//This just works, trust me! :D
				if (printPosts($exec) == 0){
				    if (isset($set)){
					echo "So far you haven't sent any messages matching the Tag '".$set."'.";
				    } else {
					echo "You haven't sent any messages so far!";
				    }
				}

				function printPosts($posts){
				    $count			= 0;
				    include("scripts/dbconnect.php");
				    while($row			= mysqli_fetch_array($posts)){
					$count++;
					echo "<form action=\"mysentmessages.php\" method=\"post\" onsubmit=\"return ask2confirm()\" >
					    <input type=\"hidden\" name=\"postId\" value=\"".$row['MsgId']."\" id=\"".$row['MsgId']."\" />
					    <input type=\"submit\" name=\"Button\" value=\"delete\" />
					</form>";
					echo "<h3>".$row['Title']."</h3>";
					echo "<p>".$row['Message']."</p>";
					echo $row['TimeStamp']."<br />";
					$query2			= "SELECT Tag FROM TagNames WHERE TaNaId in (SELECT TaNaId FROM Tags WHERE MsgId = '".$row['MsgId']."')";
					$exec2			= $db->query($query2);
					while($row2			= mysqli_fetch_array($exec2)){
					    echo "<a href=\"mysentmessages.php?tag=".stripslashes($row2['Tag'])."\">".stripslashes($row2['Tag'])."</a> ";
					}
					echo "<hr />";
				    }
				    return $count;
				}

			?>
		    </div>
		<div class="clear"></div>

		</div>
		<!-- End Right Column -->

		<div class="clear"></div>

	    </div>	   
	    <!-- End Faux Columns --> 
	    <!-- Begin Footer -->
	    <div id="footer">
		<?php
		include("include/footer.html");
		?>		
	    </div>
	    <!-- End Footer -->
	</div>
	<!-- End Wrapper -->
    </body>
</html>
